home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / amitcp / netinet / in_cksum.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  6KB  |  189 lines

  1. RCS_ID_C="$Id: in_cksum.c,v 1.7 1993/06/04 11:16:15 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * HISTORY
  8.  * $Log: in_cksum.c,v $
  9.  * Revision 1.7  1993/06/04  11:16:15  jraja
  10.  * Fixes for first public release.
  11.  *
  12.  * Revision 1.6  1993/05/17  00:16:44  ppessi
  13.  * Changed RCS version. Added rcsid.
  14.  *
  15.  * Revision 1.5  1993/04/05  19:05:52  jraja
  16.  * Changed storage of the spl functions  return values to type spl_t.
  17.  * Added include for conf.h to every .c file.
  18.  *
  19.  * Revision 1.4  93/03/19  14:32:56  14:32:56  too (Tomi Ollila)
  20.  * Chde changes at night 17-18 March 1993
  21.  * 
  22.  * Revision 1.3  93/03/05  21:09:28  21:09:28  jraja (Jarno Tapio Rajahalme)
  23.  * Fixed includes (again).
  24.  * 
  25.  * Revision 1.2  93/02/26  08:54:44  08:54:44  jraja (Jarno Tapio Rajahalme)
  26.  * Changed in_cksum_c to in_cksum so it links now.
  27.  * Made this compile with ANSI C.
  28.  * 
  29.  * Revision 1.1  92/11/17  16:28:49  16:28:49  jraja (Jarno Tapio Rajahalme)
  30.  * Initial revision
  31.  *
  32.  */
  33.  
  34. /*
  35.  * Copyright (c) 1988 Regents of the University of California.
  36.  * All rights reserved.
  37.  *
  38.  * Redistribution and use in source and binary forms, with or without
  39.  * modification, are permitted provided that the following conditions
  40.  * are met:
  41.  * 1. Redistributions of source code must retain the above copyright
  42.  *    notice, this list of conditions and the following disclaimer.
  43.  * 2. Redistributions in binary form must reproduce the above copyright
  44.  *    notice, this list of conditions and the following disclaimer in the
  45.  *    documentation and/or other materials provided with the distribution.
  46.  * 3. All advertising materials mentioning features or use of this software
  47.  *    must display the following acknowledgement:
  48.  *    This product includes software developed by the University of
  49.  *    California, Berkeley and its contributors.
  50.  * 4. Neither the name of the University nor the names of its contributors
  51.  *    may be used to endorse or promote products derived from this software
  52.  *    without specific prior written permission.
  53.  *
  54.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  55.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  58.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  59.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  60.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  61.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  62.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  63.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  64.  * SUCH DAMAGE.
  65.  *
  66.  *    @(#)in_cksum.c    7.3 (Berkeley) 6/28/90
  67.  */
  68.  
  69. #include <conf.h>
  70.  
  71. #include <sys/param.h>
  72. #include <sys/systm.h>
  73. #include <sys/malloc.h>
  74. #include <sys/mbuf.h>
  75.  
  76. #include <netinet/in_cksum_protos.h>
  77.  
  78. /*
  79.  * Checksum routine for Internet Protocol family headers (Portable Version).
  80.  *
  81.  * This routine is very heavily used in the network
  82.  * code and should be modified for each CPU to be as fast as possible.
  83.  */
  84.  
  85. #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
  86. #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
  87.  
  88. int
  89. in_cksum(m, len)
  90.     register struct mbuf *m;
  91.     register int len;
  92. {
  93.     register u_short *w;
  94.     register int sum = 0;
  95.     register int mlen = 0;
  96.     int byte_swapped = 0;
  97.  
  98.     union {
  99.         char    c[2];
  100.         u_short    s;
  101.     } s_util;
  102.     union {
  103.         u_short s[2];
  104.         long    l;
  105.     } l_util;
  106.  
  107.     for (;m && len; m = m->m_next) {
  108.         if (m->m_len == 0)
  109.             continue;
  110.         w = mtod(m, u_short *);
  111.         if (mlen == -1) {
  112.             /*
  113.              * The first byte of this mbuf is the continuation
  114.              * of a word spanning between this mbuf and the
  115.              * last mbuf.
  116.              *
  117.              * s_util.c[0] is already saved when scanning previous 
  118.              * mbuf.
  119.              */
  120.             s_util.c[1] = *(char *)w;
  121.             sum += s_util.s;
  122.             w = (u_short *)((char *)w + 1);
  123.             mlen = m->m_len - 1;
  124.             len--;
  125.         } else
  126.             mlen = m->m_len;
  127.         if (len < mlen)
  128.             mlen = len;
  129.         len -= mlen;
  130.         /*
  131.          * Force to even boundary.
  132.          */
  133.         if ((1 & (int) w) && (mlen > 0)) {
  134.             REDUCE;
  135.             sum <<= 8;
  136.             s_util.c[0] = *(u_char *)w;
  137.             w = (u_short *)((char *)w + 1);
  138.             mlen--;
  139.             byte_swapped = 1;
  140.         }
  141.         /*
  142.          * Unroll the loop to make overhead from
  143.          * branches &c small.
  144.          */
  145.         while ((mlen -= 32) >= 0) {
  146.             sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  147.             sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  148.             sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  149.             sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  150.             w += 16;
  151.         }
  152.         mlen += 32;
  153.         while ((mlen -= 8) >= 0) {
  154.             sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  155.             w += 4;
  156.         }
  157.         mlen += 8;
  158.         if (mlen == 0 && byte_swapped == 0)
  159.             continue;
  160.         REDUCE;
  161.         while ((mlen -= 2) >= 0) {
  162.             sum += *w++;
  163.         }
  164.         if (byte_swapped) {
  165.             REDUCE;
  166.             sum <<= 8;
  167.             byte_swapped = 0;
  168.             if (mlen == -1) {
  169.                 s_util.c[1] = *(char *)w;
  170.                 sum += s_util.s;
  171.                 mlen = 0;
  172.             } else
  173.                 mlen = -1;
  174.         } else if (mlen == -1)
  175.             s_util.c[0] = *(char *)w;
  176.     }
  177.     if (len)
  178.         printf("cksum: out of data\n");
  179.     if (mlen == -1) {
  180.         /* The last mbuf has odd # of bytes. Follow the
  181.            standard (the odd byte may be shifted left by 8 bits
  182.            or not as determined by endian-ness of the machine) */
  183.         s_util.c[1] = 0;
  184.         sum += s_util.s;
  185.     }
  186.     REDUCE;
  187.     return (~sum & 0xffff);
  188. }
  189.